package history.tencent1.problem1;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 *
 */
public class Solution {
    /**
     * 代码中的类名、方法名、参数名已经指定，请勿修改，直接返回方法规定的值即可
     * 你需要返回m个指针，第i个指针指向一条链，表示第i个问题的答案
     * @param root TreeNode类 指向链表树的根
     * @param b int整型一维数组 表示每个问题是什么
     * @return ListNode类一维数组
     */
    public ListNode[] solve (TreeNode root, int[] b) {
        // map里存着所有路径
        ListNode[] res = new ListNode[b.length];
        int resCount = 0;
        Map<Integer, Integer> routeMap = new HashMap<>();
        addToMap(root, routeMap);
        System.out.println(routeMap);
        for(int element: b){
            System.out.println(b);
            List<Integer> tempList = new ArrayList<>();
            int tempElement = element;
            tempList.add(tempElement);
            while(routeMap.containsKey(tempElement)){
                System.out.println("while");
                tempList.add(routeMap.get(tempElement));
                tempElement = routeMap.get(tempElement);
            }
            ListNode head = new ListNode(-1);
            ListNode tempHead = head;
            for(int i = tempList.size() - 1; i >= 0; i--){
                tempHead.next = new ListNode(tempList.get(i));
                tempHead = tempHead.next;
            }
            res[resCount++] = head.next;
        }
        System.out.println("end");
        return res;
    }
    public void addToMap(TreeNode node, Map<Integer, Integer> routeMap){
        if(node.left != null){
            routeMap.put(node.left.val, node.val);
            addToMap(node.left, routeMap);
        }
        if(node.right != null){
            routeMap.put(node.right.val, node.val);
            addToMap(node.right, routeMap);
        }
    }

}
class TreeNode {
    int val = 0;
    TreeNode left = null;
   TreeNode right = null;
   public TreeNode(int val) {
     this.val = val;
   }
}
class ListNode {
    int val;
    ListNode next = null;

    public ListNode(int val) {
        this.val = val;
    }
}